﻿Public MustInherit Class TableMortaliteIntegreeActuariat
    Implements Actuariat.ITableMortalite

    Public Sub New(ByVal Nom As String)
        m_Nom = Nom
    End Sub

    Private m_Nom As String
    Public ReadOnly Property Nom As String Implements ITableMortalite.Nom
        Get
            Return m_Nom
        End Get
    End Property

    'La colonne 0 contient les âges (en années). La colonne 1 contient les taux de mortalité
    Private m_Values As Double(,)
    Public ReadOnly Property Values As Double(,) Implements ITableMortalite.Values
        Get
            If m_Values Is Nothing Then

                Dim ValuesNbVivants() As Double = TableauNbVivants()
                Dim ValuesMortalite(ValuesNbVivants.Count - 1, 1) As Double
                For i As Integer = 0 To ValuesNbVivants.Count - 1

                    ValuesMortalite(i, 0) = i

                    If ValuesNbVivants(i) <> 0 Then
                        ValuesMortalite(i, 1) = (ValuesNbVivants(i) - ValuesNbVivants(i + 1)) / ValuesNbVivants(i)
                    Else
                        ValuesMortalite(i, 1) = 1
                    End If

                Next
                m_Values = ValuesMortalite

            End If
            Return m_Values
        End Get
    End Property

    'Recherche l'âge avec le taux de mortalité inférieur et le plus proche de QxAn
    Public Function AgeAnMinFromQxAn(ByVal QxAn As Double) As Integer Implements ITableMortalite.AgeAnMinFromQxAn
        Dim result As Integer, ecart_min As Double = Double.PositiveInfinity
        For i As Integer = 0 To m_Values.GetUpperBound(0)
            If m_Values(i, 1) < QxAn And Math.Abs(m_Values(i, 1) - QxAn) < ecart_min Then
                result = m_Values(i, 0)
                ecart_min = Math.Abs(m_Values(i, 1) - QxAn)
            End If
        Next
        Return result
    End Function

    ''' <summary>
    ''' Tableau des nombres de vivants par âges TableauNbVivants(i) contient le nombre de vivants à l'âge i
    ''' </summary>
    Protected MustOverride Function TableauNbVivants() As Double()

    Public ReadOnly Property DataTable As System.Data.DataTable Implements ITableMortalite.DataTable
        Get
            Throw New NotImplementedException
        End Get
    End Property

    Public ReadOnly Property TxMortalite(ByVal age As Integer) As Object Implements ITableMortalite.TxMortalite
        Get
            Return m_Values(age, 1)
        End Get
    End Property
End Class
